#!/bin/bash

set -e          # exit on error
set -o pipefail # exit on pipeline error
set -u          # treat unset variable as error

. /minioslib || exit 1

SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"

LNG="$(echo $LOCALE | cut -d_ -f1)"

pkg update

echo "nodm nodm/enabled boolean true" | debconf-set-selections
echo "samba-common samba-common/dhcp boolean false" | debconf-set-selections

install_packages --package-list "$SCRIPT_DIR/$PACKAGE_VARIANT.list"

if [ $LOCALE != "en_US" ]; then
    install_packages --package-list "$SCRIPT_DIR/$PACKAGE_VARIANT-l10n.list"
fi

if [ -f /usr/share/applications/yad-icon-browser.desktop ]; then
    rm -f /usr/share/applications/yad-icon-browser.desktop
fi

rm -f /usr/share/applications/minios-installer.desktop

if grep -q slim $SCRIPT_DIR/$PACKAGE_VARIANT.list && ! grep -q "#slim" $SCRIPT_DIR/$PACKAGE_VARIANT.list; then
    sed -i 's,# hidecursor          false,hidecursor            false,g' /etc/slim.conf
    sed -i 's,screenshot_cmd      scrot /root/slim.png,# screenshot_cmd      scrot /root/slim.png,g' /etc/slim.conf
    sed -i 's,#auto_login          no,auto_login          yes,g' /etc/slim.conf
    sed -i 's,current_theme       debian-softwaves,current_theme       minios,g' /etc/slim.conf
    sed -i "s/#default_user        simone/default_user        $USER_NAME/g" /etc/slim.conf
fi

if grep -q nodm $SCRIPT_DIR/$PACKAGE_VARIANT.list && ! grep -q "#nodm" $SCRIPT_DIR/$PACKAGE_VARIANT.list; then
    cat <<EOF >/etc/default/nodm
# nodm configuration

# Set NODM_ENABLED to something different than 'false' to enable nodm
NODM_ENABLED=true

# User to autologin for
NODM_USER=live

# First vt to try when looking for free VTs
NODM_FIRST_VT=7

# X session
NODM_XSESSION=/etc/X11/Xsession

# Options for nodm itself
NODM_OPTIONS=

# Options for the X server.
#
# Format: [/usr/bin/<Xserver>] [:<disp>] <Xserver-options>
#
# The Xserver executable and the display name can be omitted, but should
# be placed in front, if nodm's defaults shall be overridden.
NODM_X_OPTIONS='-nolisten tcp'

# If an X session will run for less than this time in seconds, nodm will wait an
# increasing bit of time before restarting the session.
NODM_MIN_SESSION_TIME=60

# Timeout (in seconds) to wait for X to be ready to accept connections. If X is
# not ready before this timeout, it is killed and restarted.
NODM_X_TIMEOUT=300

EOF
fi

if ! grep -q pulseaudio $SCRIPT_DIR/$PACKAGE_VARIANT.list || grep -q "#slim" $SCRIPT_DIR/$PACKAGE_VARIANT.list; then
    cat <<EOF >/usr/bin/audio-allowuser.sh
#!/bin/bash
if ! grep audio /etc/group | grep \$(id -nu 1000) >/dev/null; then
    usermod -a -G audio \$(id -nu 1000)
fi
EOF
    chmod +x /usr/bin/audio-allowuser.sh
    cat <<EOF >/usr/lib/systemd/system/audio-allowuser.service
[Unit]
Description=Allow user to use ALSA
#After=network.target

[Service]
Type=oneshot
ExecStart=/usr/bin/audio-allowuser.sh
RemainAfterExit=true
ExecStop=
StandardOutput=journal

[Install]
WantedBy=multi-user.target
EOF
    systemctl enable audio-allowuser.service
fi
